// pages/go_wu_che_gopay/go_wu_che_gopay.js
import {request} from "../../utils/http.js"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    dingdan:[],
    money:0,
    steps: [
      {
        text: "提交订单成功",
      },
      {
        text: "微信付款",
      },
    ],
    active: 0, // 当前步骤
    radio: 'wechatPay', // 默认选中微信支付
    showPopup: false,//是否弹出
    showKeyboard: false, // 是否显示数字键盘
    password: ['', '', '', '', '', ''], // 密码输入状态
    currentIndex: 0, // 当前输入的位置,
    correctPassword: "123456" ,// 正确密码
    keys: [
      { label: '1', key: 1, bgColor: 'white' },
      { label: '2', key: 2, bgColor: 'white' },
      { label: '3', key: 3, bgColor: 'white' },
      { label: '4', key: 4, bgColor: 'white' },
      { label: '5', key: 5, bgColor: 'white' },
      { label: '6', key: 6, bgColor: 'white' },
      { label: '7', key: 7, bgColor: 'white' },
      { label: '8', key: 8, bgColor: 'white' },
      { label: '9', key: 9, bgColor: 'white' },
      { label: '', key: '', bgColor: '#f7f7f7' },
      { label: '0', key: 0, bgColor: 'white' },
      { label: '×', key: 'x', bgColor: '#f7f7f7' }
    ]
    


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const dingdan=wx.getStorageSync("dingdan")
    this.setData({
      dingdan:dingdan,
      money:options.money
    })
    console.log(dingdan);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onChange(event) {
    this.setData({
      radio: event.detail,
    });
  },
  onPay(){
    this.setData({
      showPopup: true,
      showKeyboard: true
    })
  },
  onClose(){
    this.setData({
      showPopup: false,
      showKeyboard: false
    })
  },
  onback(){
    wx.reLaunch({
      url:"/pages/go_wu_che_fenlei/go_wu_che_fenlei"
    })
  },
  // 键盘点击事件
  onKeyPress(e) {
    const { key } = e.currentTarget.dataset;
    let { password, currentIndex } = this.data;

    if (key === "x") {
      // 删除密码
      if (currentIndex > 0) {
        password[currentIndex - 1] = '';
        currentIndex--;
      }
    } else if (key !== "") {
      // 输入密码
      if (currentIndex < 6) {
        password[currentIndex] = key;
        currentIndex++;
      }
    }

    this.setData({
      password,
      currentIndex
    });

    // 如果密码输入完成
    if (currentIndex === 6) {
      this.submitPassword();
    }
  },

  // 提交密码
  submitPassword() {
    const password = this.data.password.join('');
    console.log(password);
    // 此处可发起支付请求逻辑
    this.matchPassword(password);
  },
  matchPassword(password){
    request({
      url:"/dianshang/money/list?id="+getApp().globalData.userInfo.userId,
      header: {
        "authorization": getApp().globalData.authorization
      }
    }).then(data=>{
      if(data.rows[0].password==password&&data.rows[0].money>=this.data.money){
        wx.showModal({
          title: "支付成功，点击返回主页",
          showCancel: false,
          success(){
            wx.reLaunch({
              url:"/pages/shou_ye/shou_ye"
            })
          }
        });
        this.data.dingdan.forEach(id=>{
          request({
            url:"/dianshang/orders",
            method:'PUT',
            data:{
              id:id,
              status:2
            },
            header: {
              "authorization": getApp().globalData.authorization
            }
          })
          request({
            url:"/dianshang/money",
            method:"PUT",
            data:{
              id:getApp().globalData.userInfo.userId,
              money:data.rows[0].money-this.data.money
            },
            header: {
              "authorization": getApp().globalData.authorization
            }
          })
          request({
            url:"/dianshang/orders/listGoods?id="+id,
            header: {
              "authorization": getApp().globalData.authorization
            },
            method:"GET"
          }).then(data=>{
            console.log("------------");
            console.log(data);
            request({
              url:"/dianshang/goods",
              method:"PUT",
              data:{
                id:data.rows[0].aGoods.id,
                totalsale:data.rows[0].aGoods.totalsale+data.rows[0].nums
              },
              header: {
                "authorization": getApp().globalData.authorization
              }
            }).then(data=>{
              console.log(data);
            })
          })
        })

      }else if(data.rows[0].password!=password){
        wx.showModal({
          title: "密码错误",
          showCancel: false
        });
        this.resetPassword();
      }else{
        wx.showModal({
          title: "余额不足",
          showCancel: false
        });
      }
    })
  },
    // 重置密码输入
  resetPassword() {
    this.setData({
      password: [],
      currentIndex: 0
    });
  }

  

})